import store from '@/store';
const request = require('request');
// import { mapState } from 'vuex';
// const { besrurl:res } = {...mapState('http',['besrurl'])}

// const teatUrl = "http://43.142.41.90:8081"
// const teatUrl ="http://127.0.0.1:8082"

const get = (url, data, msg) => {
	const {
		besrurl: teatUrl
	} = store.state.http
	if (teatUrl) {
		let que = /^((https|http)?:\/\/)[^\s]+/;
		return new Promise((res, rej) => {
			request({
					url: que.test(url) ? url : teatUrl + url,
					method: "GET",
					data: {
						...data
					}
				},
				function(error, response, body) {
					if (body) {
						const data = JSON.parse(body)
						res(data)
					} else {
						res({
							code: -1,
							message: "异常"
						})
					}


				})
		})
	}

}

/* const getMod = (data) => {
	return new Promise((res, rej) => {
		let arr = [];
		arr.push(parseInt(data));
		console.log([parseInt(data)]);
		request({
				url: "http://db.steamworkshopdownloader.io/prod/api/details/file",
				// url: "http://www.baidu.com",
				json: true,
				// body: JSON.stringify(data),
				body: arr,
				method: "POST",
				headers: {
					'Content-Type': 'application/json;charset=UTF-8',
					// 'Content-Type': 'text/plain',
					// 'mode': "no-cors",
					// 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
				}
			},
			function(error, response, body) {
				console.log("`````````````````````````````````");
				console.log(error);
				console.log(response);
				res(body)
			})
			console.log(2111111111111);
	})
} */

const post = (url, data, msg) => {

	const {
		besrurl: teatUrl
	} = store.state.http
	if (teatUrl) {
		let que = /^((https|http)?:\/\/)[^\s]+/;
		return new Promise((res, rej) => {
			var request = require('request');
			request({
					url: que.test(url) ? url : teatUrl + url,
					json: true,
					// body: JSON.stringify(data),
					body: data,
					method: "POST",
					header: {
						'Content-Type': 'application/json;charset=UTF-8',
						// 'Content-Type': 'multipart/form-data;charset=UTF-8'
					}
				},
				function(error, response, body) {
					if (body) {
						res(body)
					} else {
						rej({
							code: -1,
							message: "异常",
						})
					}
				})
		})
	}
}

export default {
	get,
	post
}